﻿using System;
using System.Collections.Generic;
using UnityEngine;

class MainPlayer : SceneFightObject
{
    public int id = 1;
    public int MaxHealth { private set; get; }
    public Vector3 InitPoint { private set; get; }

    private void Start()
    {
        Mgr.mainPlayer = this;
        info = new PlayerInfo
        {
            Attack = 20,
            Defense = 10,
            Health = 200,
            Name = "MianPlayer"
        };
        InitPoint = transform.position;

        MaxHealth = info.Health;

        Mgr.fightMgr.fightEvent.DispatchEvent(FightEvent.PlayerChangeHp, new int[2]{ info.Health, MaxHealth });

        Mgr.fightMgr.fightEvent.AddListener(FightEvent.RESMONSTERATTACKPLAYER, ResMonsterAttackHandler);
        Mgr.fightMgr.fightEvent.AddListener(FightEvent.PlayerChangeHp, PlayerChangeHpHandler);
    }


    //接收到被怪物攻击消息
    private void ResMonsterAttackHandler(object data)
    {
        AttackInfo attackInfo = data as AttackInfo;

        //伤害需要>=0
        int damage = Math.Max(0, attackInfo.Damage - info.Defense);
        //生命值需要>=0
        info.Health = Math.Max(0, info.Health - damage);
        Mgr.fightMgr.fightEvent.DispatchEvent(FightEvent.PlayerChangeHp, new int[2] { info.Health, MaxHealth });
    }

    private void PlayerChangeHpHandler(object data)
    {
        int[] hpArr = data as int[];
        info.Health = hpArr[0];
    }

    private void Update()
    {
        if(isDeadState)
        {
            Time.timeScale = 0;
            Mgr.panelMgr.ShowPanel(EnumPanelType.GameOverPanel);
        }
    }


    public override void Recycle()
    {
        
    }
}